package com.mall.manager.userpart.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.WebUtils;

import com.mall.untils.Page;
/**用户角色控制接口
 * @author zhaopeisen
 *
 */
@Service("manageuserpartService")
public class ManageUserPartService {
	 	private Logger logger = LoggerFactory.getLogger(ManageUserPartService.class);

	    @Resource
	    private JdbcTemplate jdbcTemplate;

		/**用户分配角色
		 * @param paraMap
		 * @return
		 */
		public int manager_userpart_save(Map<String, Object> paraMap) {
			int user_id = MapUtils.getIntValue(paraMap, "user_id");
			int part_id = MapUtils.getIntValue(paraMap, "part_id");
			try {
				//验证该用户是否已经有了该角色
				String sql = "select count(*) from t_user_part where part_id="+part_id+" and user_id="+user_id;
	 	        int total = this.jdbcTemplate.queryForInt(sql);
	 	        if(total>0){
	 	        	return -1;
	 	        }
				String sqlI="insert into t_user_part(user_id,part_id,create_time,update_time) values(?,?,now(),now())";
				int update = jdbcTemplate.update(sqlI,user_id,part_id);
				return update;
			} catch (Exception e) {
				logger.info("用户分配角色失败！");
				return 0;
			}
		}


		/**按照用户显示列表
		 * @param request
		 * @return
		 */
		public Page manager_listbyUser(HttpServletRequest request) {
			Page page = null;
		    Map paraMap = WebUtils.getParametersStartingWith(request, "");
		    int pageNo = MapUtils.getIntValue(paraMap, "pageNo");
		    int part_id = MapUtils.getIntValue(paraMap, "part_id");
		    int pageSize = 10;
	    	try {
	 	        if (pageNo == 0){
	 	        	pageNo = 1;
	 	        }
	 	        String sql = "select count(*) from t_user_part where part_id="+part_id;
	 	        int total = this.jdbcTemplate.queryForInt(sql);
	 	        int startIndex = Page.getStartOfPage(pageNo, pageSize);
	 	        sql = "select u from t_user_part up join t_user u on up.user_id=u.id where up.part_id="+part_id+" order by u.id desc limit " + startIndex + "," + pageSize;
	 	        List queryForList = this.jdbcTemplate.queryForList(sql);
	 	        page = new Page(startIndex, total, pageSize, queryForList);
	 	        return page;
	 	    } catch (Exception e) {
	 	    	logger.info("角色用户显示失败！");
	 	        return null;
	 	    }
		}
		
		/**按照角色显示列表
		 * @param request
		 * @return
		 */
		public List manager_listbypart(HttpServletRequest request) {
		    Map paraMap = WebUtils.getParametersStartingWith(request, "");
		    int user_id = MapUtils.getIntValue(paraMap, "user_id");
	    	try {
	    		String sql = "";
	 	       if(user_id== 0){
		   	    	  return null;
		   	   }else{  	    
	 	        sql = "select p.part_name,p.id from t_user_part up join t_part p on up.part_id=p.id where up.user_id="+user_id;
		   	   }
	 	        List queryForList = this.jdbcTemplate.queryForList(sql);
	 	       return queryForList;
	 	    } catch (Exception e) {
	 	        this.logger.info("用户角色显示失败！");
	 	        return null;
	 	    }
		}

		/**删除用户角色
		 * @param paraMap
		 * @return
		 */
		public int manager_userpart_delete(Map<String, Object> paraMap) {
			int user_id = MapUtils.getIntValue(paraMap, "user_id");
			int part_id = MapUtils.getIntValue(paraMap, "part_id");
			try {
				String sql="delete from t_user_part where user_id=? and part_id=?";
				int update = jdbcTemplate.update(sql,user_id,part_id);
				return update;
				
			} catch (Exception e) {
				e.printStackTrace();
				this.logger.info("用户角色删除失败！");
				return 0;
			}
		}
			
		/**更新用户角色
		 * @param paraMap
		 * @return
		 */
		@Transactional
		public int manager_power_update(Map<String, Object> paraMap) {
			int user_id = MapUtils.getIntValue(paraMap, "user_id");
			String part_id = MapUtils.getString(paraMap, "part_id");
			//对part_id字符串拆分
			if(part_id.equals("")){
				String sql="delete from t_user_part where user_id=?";
				jdbcTemplate.update(sql,user_id);
				return 1;
			}
			String[] part_ids = part_id.split(",");
			int part;
				try {
					if(part_ids.length>0){
						String sql="delete from t_user_part where user_id=?";
						jdbcTemplate.update(sql,user_id);
						for(int i=0;i<part_ids.length;i++){
							
							part = Integer.parseInt(part_ids[i]);
							String sqlI="insert into t_user_part(user_id,part_id,create_time,update_time) values(?,?,now(),now())";
						    jdbcTemplate.update(sqlI,user_id,part);
						}
						
						return 1;
					}else{
						String sql="delete from t_user_part where user_id=?";
						jdbcTemplate.update(sql,user_id);
						return 1;
					}
				} catch (Exception e) {
					this.logger.info("用户角色更新失败！");
					e.printStackTrace();
					return 0;
				}
		}
}
